//
//  SMNewsDetailViewController.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/22.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "SMNewsDetailViewController.h"
#import "SMNewsTextViewCell.h"
#import "SMNewsImageViewCell.h"
#import "SMNewsTableViewCell.h"
#import "SMNewsHeadViewCell.h"

@interface SMNewsDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *newsDetailTableView;
@property(nonatomic, strong) NSArray *headArray;
@property(nonatomic, strong) NSArray *touristArray;
@property(nonatomic, strong) NSArray *recommandlist;

@end

@implementation SMNewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.newsDetailTableView.estimatedRowHeight = 10;
    [self.newsDetailTableView registerNib:[UINib nibWithNibName:[SMNewsTextViewCell className] bundle:nil] forCellReuseIdentifier:@"SMNewsTextViewCell"];
    [self.newsDetailTableView registerNib:[UINib nibWithNibName:[SMNewsImageViewCell className] bundle:nil] forCellReuseIdentifier:@"SMNewsImageViewCell"];
    [self.newsDetailTableView registerNib:[UINib nibWithNibName:[SMNewsTableViewCell className] bundle:nil] forCellReuseIdentifier:@"SMNewsTableViewCell"];
    [self.newsDetailTableView registerNib:[UINib nibWithNibName:@"SMNewsHeadViewCell" bundle:nil] forCellReuseIdentifier:@"SMNewsHeadViewCell"];
    [[YDNetClient sharedInstance] getMethod:self.newsDetailURL param:nil success:^(id object) {
        self.headArray = object[@"header"];
        self.touristArray = object[@"body"];
        self.recommandlist = [SMNewsModel mj_objectArrayWithKeyValuesArray:object[@"recommandlist"]];
        [self.newsDetailTableView reloadData];
    } fail:^(NSError *error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        SMNewsDetailViewController *vc = MainStoryBoardVC(@"SMNewsDetailViewController");
        SMNewsModel *model = self.recommandlist[indexPath.row];
        vc.newsDetailURL = model.detailUrl;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return self.headArray.count;
    if (section == 1) return self.touristArray.count;
    if (section == 2) return self.recommandlist.count;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SMNewsHeadViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMNewsHeadViewCell" forIndexPath:indexPath];
        NSDictionary *dic = self.headArray[indexPath.row];
        cell.titielLb.text = dic[@"name"];
        cell.sourceLb.text = dic[@"channle"];
        return cell;
    } else if (indexPath.section == 1) {
        NSDictionary *dic = self.touristArray[indexPath.row];
        if ([dic[@"type"] isEqualToString:@"image"]) {
            SMNewsImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMNewsImageViewCell" forIndexPath:indexPath];
            @weakify(cell)
            [cell.touristImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"imageurl"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                @strongify(cell)
                float scale = image.size.width / image.size.height;
                cell.imageViewHeight.constant = kScreenW / scale;
            }];
            return cell;
        } else {
            SMNewsTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMNewsTextViewCell" forIndexPath:indexPath];
            cell.descriLb.text = dic[@"textcontent"];
            return cell;
        }
    } else {
        SMNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMNewsTableViewCell"];
        SMNewsModel *newsModel = self.recommandlist[indexPath.row];
        [cell setNewsCellModel:newsModel];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        UIView *view = ALLOC(UIView);
        view.backgroundColor = UIColorHex(ffffff);
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, kScreenW, 38)];
        lb.textColor = UIColorHex(EA7625);
        lb.font = [UIFont boldSystemFontOfSize:16];
        lb.text = @"推荐";
        lb.textAlignment = NSTextAlignmentCenter;
        UILabel *lines = [[UILabel alloc] initWithFrame:CGRectMake(0, 39.5, kScreenW, 0.5)];
        lines.backgroundColor = UIColorHex(eaeaea);
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 0.5)];
        line.backgroundColor = UIColorHex(eaeaea);
        [view addSubview:lines];
        [view addSubview:lb];
        [view addSubview:line];
        return view;
    }
    return ALLOC(UIView);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) return 40;
    return 0.000001;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

